home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / mpmod160.zip / SOURCE.ZIP / COMPRESS.C next >
Text File  |  1994-01-01  |  4KB  |  98 lines

  1. /*---------------------------------------------------------------------------*/
  2. /* Compress.c - implements compression based on code by Richard B. Johnson   */
  3. /*              and utilises the existing Zmodem send/receive routines by    */
  4. /*              recognising special packets.                                 */
  5. /*                                                                           */
  6. /*              See the other source (extract) files for information on how  */
  7. /*              to implement this code into your version of Zmodem.          */
  8. /*                                                                           */
  9. /* This source code (and its header file) is submitted to the public with no */
  10. /* warranty for its merchantibility or fitness for a particular purpose. You */
  11. /* are permitted to use these routines in your version of Zmodem without     */
  12. /* payment of any kind to me.                                                */
  13. /*                                                                           */
  14. /* <Written by: M. Jose 93-09-28>                                            */
  15. /*---------------------------------------------------------------------------*/
  16.  
  17. int Compressed;             /* For detecting if the packet was sent with it. */
  18. BYTE *CompBuff;             /* Compression buffer                            */
  19.  
  20.  
  21. /*---------------------------------------------------------------------------*/
  22. /* This routine does the actual compression of the block (BufferIn) and      */
  23. /* places the output into BufferOut. A constant check is kept and if the     */
  24. /* amount of bytes in the compressed packet exceeds the number in the un-    */
  25. /* compressed packet, the routine is aborted. This ensures that Zmodem does  */
  26. /* not send packets larger than it should. (For obvious reasons!).           */
  27. /*---------------------------------------------------------------------------*/
  28. int pascal Compress(register BYTE *BufferIn, register BYTE *BufferOut, register int length)
  29. {
  30.    static unsigned short NumDupes;
  31.    static unsigned short count;
  32.    static BYTE Duplicate;
  33.    static int Blocksize;
  34.  
  35.    NumDupes = count = 0;
  36.    Blocksize = length;       /* Make sure compressed length doesn't exceed */
  37.                              /* the uncompressed length.                   */
  38.  
  39.    while (length) {
  40.       if ((*BufferIn == (BYTE )0xBB) || (*BufferIn == *(BufferIn+1))) {
  41.          *BufferOut++ = (BYTE )0xBB;
  42.          Duplicate = *BufferIn;
  43.          NumDupes = 0;
  44.          while ((*BufferIn++ == Duplicate) && length) {
  45.             NumDupes++;
  46.             length--;
  47.          }
  48.          *BufferOut++ = (BYTE ) NumDupes;
  49.          *BufferOut++ = (BYTE )(NumDupes >> 8);
  50.          *BufferOut++ = Duplicate;
  51.          count += 4;
  52.          BufferIn--;
  53.       } else {
  54.          *BufferOut++ = *BufferIn++;
  55.          count++;
  56.          length--;
  57.       }
  58.       if (count > Blocksize)
  59.          return (Blocksize + 99);           /* Don't send this compressed */
  60.    }
  61.    return count;
  62. }
  63.  
  64.  
  65. /*---------------------------------------------------------------------------*/
  66. /* This routine takes the compressed buffer (BufferIn) and places the        */
  67. /* decoded data into the buffer (BufferOut). The resultant buffer size will  */
  68. /* be the same as the uncompressed size of the transmitted block.            */
  69. /*---------------------------------------------------------------------------*/
  70. int pascal DeCompress(register BYTE *BufferIn, register BYTE *BufferOut, int length)
  71. {
  72.    static unsigned short NumDupes;
  73.    static unsigned short count;
  74.    static BYTE ch, *limit;
  75.  
  76.    NumDupes = count = 0;
  77.  
  78.    limit = BufferIn + length;
  79.  
  80.    while (BufferIn < limit) {
  81.       if (*BufferIn == (BYTE )0xBB) {
  82.          BufferIn++;
  83.          NumDupes = (unsigned short )*BufferIn++;
  84.          NumDupes = NumDupes | (unsigned short ) (*BufferIn++ << 8);
  85.  
  86.          ch = *BufferIn++;
  87.          for (; NumDupes > 0; NumDupes--) {
  88.             *BufferOut++ = ch;
  89.             count++;
  90.          }
  91.       } else {
  92.          *BufferOut++ = *BufferIn++;
  93.          count++;
  94.       }
  95.    }
  96.    return count;        /* Will return current Zmodem blocksize */
  97. }
  98.